package com.hospital.service.impl;

import com.alipay.api.domain.ItemInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.configure.Result;
import com.hospital.entity.HealthInfo;
import com.hospital.entity.HealthItem;
import com.hospital.entity.HealthItemInfo;
import com.hospital.mapper.HealthInfoMapper;
import com.hospital.mapper.HealthItemInfoMapper;
import com.hospital.service.HealthInfoService;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-03-24
 */
@Service
public class HealthInfoServiceImpl extends ServiceImpl<HealthInfoMapper, HealthInfo> implements HealthInfoService {

    @Resource
    HealthInfoMapper healthInfoMapper;

    @Resource
    HealthItemInfoMapper healthItemInfoMapper;

    @Override
    public Result<HealthInfo> updateHealthInfo(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();
        try {
            if (StringUtils.isEmpty(healthInfo.getInfoId())) {
                //如果ID为空，代表添加
                healthInfo.setInfoId(IDGenerator.getUUID("INFO"));
                healthInfoMapper.insert(healthInfo);

                if (healthInfo.getItemList() != null && healthInfo.getItemList().size() != 0) {
                    HealthItemInfo healthItemInfo = new HealthItemInfo();
                    for (Map item : healthInfo.getItemList()) {
                        if (item.containsKey("itemValue")) {
                            healthItemInfo.setItemInfoId(IDGenerator.getUUID("ITIN"));
                            healthItemInfo.setInfoId(healthInfo.getInfoId());
                            healthItemInfo.setItemId(item.get("itemId").toString());
                            healthItemInfo.setItemValue(Double.parseDouble(item.get("itemValue").toString()));
                            healthItemInfoMapper.insert(healthItemInfo);
                        }
                    }
                }
            } else {
                //如果ID不为空，代表修改
                healthInfoMapper.updateById(healthInfo);

                QueryWrapper<HealthItemInfo> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(HealthItemInfo::getInfoId, healthInfo.getInfoId());
                healthItemInfoMapper.delete(wrapper);

                if (healthInfo.getItemList() != null && healthInfo.getItemList().size() != 0) {
                    HealthItemInfo healthItemInfo = new HealthItemInfo();
                    for (Map item : healthInfo.getItemList()) {
                        if (item.containsKey("itemValue")) {
                            healthItemInfo.setItemInfoId(IDGenerator.getUUID("ITIN"));
                            healthItemInfo.setInfoId(healthInfo.getInfoId());
                            healthItemInfo.setItemId(item.get("itemId").toString());
                            healthItemInfo.setItemValue(Double.parseDouble(item.get("itemValue").toString()));
                            healthItemInfoMapper.insert(healthItemInfo);
                        }
                    }
                }
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthInfo> delHealthInfo(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();
        try {
            if (StringUtils.isEmpty(healthInfo.getInfoId())) {
                result.setMessage("healthInfoId can't be empty");
                return result;
            }
            healthInfoMapper.deleteById(healthInfo.getInfoId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthInfo> batchDelHealthInfo(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(healthInfo.getIds())) {
                String[] ids = healthInfo.getIds().split(",");
                for (String id : ids) {
                    healthInfoMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthInfo> queryHealthInfo(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();
        try {
            QueryWrapper<HealthInfo> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            List<HealthInfo> healthInfos = healthInfoMapper.selectList(wrapper);
            result.setReturnList(healthInfos);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthInfo> queryHealthInfoByPage(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();

        try {
            if (healthInfo.getCurrent() == null || healthInfo.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            IPage<HealthInfo> healthInfos = healthInfoMapper.queryHealthInfoByPage(new Page<>(healthInfo.getCurrent(), healthInfo.getSize()), healthInfo);
            result.setReturnPage(healthInfos);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthInfo> queryHealthInfoById(HealthInfo healthInfo) {
        Result<HealthInfo> result = new Result<>();
        try {
            HealthInfo returnHealthInfo = healthInfoMapper.selectById(healthInfo.getInfoId());
            result.setReturnObj(returnHealthInfo);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
